/**
 * 员工列表
 */
var dataTable;
var dataArray = [];
$(function(){
	
	
	//提示信息初始设置
	toastr.options = {
			  "closeButton": true,
			  "debug": false,
			  "positionClass": "toast-top-center",
			  "onclick": null,
			  "showDuration": "1000",
			  "hideDuration": "1000",
			  "timeOut": "5000",
			  "extendedTimeOut": "1000",
			  "showEasing": "swing",
			  "hideEasing": "linear",
			  "showMethod": "fadeIn",
			  "hideMethod": "fadeOut"
			};
	
	showDateTable();
	
	//新增或更新（根据id）
	$("#btn_commit_one_form").click(function(){
		//检查格式；
		if($.trim($("#name").val())==''){
			toastr.error("员工姓名必填");
		}else{
			var params=$("#one_form").serialize();
			$.ajax({
	    		url:'web/saveOrUpdateEmployee.htm',
	    		data:params,
	    		type:'post',
	    		success:function(data){
	    			if(data){
	    				toastr.success("操作成功！");
	    				dataTable.fnDraw();
	    			}else{
	    				toastr.error("操作失败！");
	    			}
	    		}
	    	});
	    }
	});
	
	//删除员工
	$("#btn_commit_delView").click(function(){
		var delId =$("#delId").val();
		if(delId!=''){
			$.ajax({
	    		url:'web/delEmployee.htm',
	    		data:{delId:delId},
	    		type:'post',
	    		success:function(data){
	    			if(data){
	    				toastr.success("操作成功！");
	    				dataTable.fnDraw();
	    			}else{
	    				toastr.error("操作失败！");
	    			}
	    		}
	    	});
		}
	});
	
	//点击查询按键提交查询
	$("#btn_search_commitBtn").click(function(){
		//dataTable.remove();
		var a,b,c,d;
		a=$.trim($("#searchName").val());
		b=$("#searchSex").val();
		c=$.trim($("#searchMobile").val());
		d=$("#searchStatus").val();
		if(dataTable!=null){
			dataTable.fnDestroy();
			showDateTable(a,b,c,d);
		}
	});
	
});


function showDateTable(a,b,c,d) {
	if (!jQuery().dataTable) {
		console.log("....")
		return;
	}
	dataTable=$('#employee_table').dataTable({
		"sDom" : "<'row'<'col-md-6 col-sm-12'l><'col-md-12 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", //default layout without horizontal scroll(remove this setting to enable horizontal scroll for the table)
		"sAjaxSource":"web/loadEmployeeList.htm",
		"fnServerData": function ( sSource, aoData, fnCallback ) {
			if(a){aoData.push({name:"name",value:a});}
			if(b){aoData.push({name:"sex",value:b});}
			if(c){aoData.push({name:"mobile",value:c});}
			if(d){aoData.push({name:"status",value:d});}
			if(a||b||c||d){//修改初始start
				for(var i=0;i<aoData.length;i++){
					if(aoData[i].name=="iDisplayStart"){
						aoData[i].value = 0;
					}
				}
			}
			$.ajax({
				"dataType":'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": fnCallback
			});
		},
		//"bDeferRender":true,
		"bFilter":false,
		"bLengthChange":true,//取消显示每页条数
		"bProcessing": true,
		"bServerSide": true,//true依据后台排序
		"bStateSave": true,
		"bJQueryUI":false,//是否用jquery样式
		"sZeroRecords": "没有检索到数据",
		"aoColumns": [
		              { 'mDataProp':'name',"bSortable": false,"sClass": "center","sTitle":"姓名"},
		              { 'mDataProp':'departmentName',"bSortable": false,"sClass": "center","sTitle":"部门"},
		              { 'mDataProp':'sex',"bSortable": false,"sClass":"center" ,"sTitle":"性别",
		            	  "mRender": function ( data, type, row ) {
		            		  if(data==undefined){
		            			  return '<span>未填写</span>';
		            		  }else{
		            			  if(data==1){
		            				  return '女';
		            			  }else{
		            				  return '男';
		            			  }
		            		  }
		            	  }
		              },
		              { 'mDataProp':'age',"bSortable": false ,"sClass": "center","sTitle":"年龄",
		            	  "mRender": function ( data, type, row ) {
		            		  if(data==undefined||data==''){
		            			  return '<span>未填写</span>';
		            		  }else{
		            			return data;
		            		  }
		            	  }
		              },
		              { 'mDataProp':'tel',"bSortable": false,"sClass": "center","sTitle":"电话",
		            	  "mRender": function ( data, type, row ) {
		            		  if(data==undefined||data==''){
		            			  return '<span>未填写</span>';
		            		  }else{
		            			return data;
		            		  }
		            	  }
		              },
		              { 'mDataProp':'mobile',"bSortable": false ,"sClass": "center","sTitle":"手机",
		            	  "mRender": function ( data, type, row ) {
		            		  if(data==undefined||data==''){
		            			  return '<span>未填写</span>';
		            		  }else{
		            			return data;
		            		  }
		            	  }
		              },
		              { 'mDataProp':'email',"bSortable": false,"sClass": "center","sTitle":"邮箱",
		            	  "mRender": function ( data, type, row ) {
		            		  if(data==undefined||data==''){
		            			  return '<span>未填写</span>';
		            		  }else{
		            			return data;
		            		  }
		            	  } 
		              },
		              /*{ 'mDataProp':'qq',"bSortable": false,"sClass": "center","sTitle":"qq",
		            	  "mRender": function ( data, type, row ) {
		            		  if(data==undefined||data==''){
		            			  return '<span>未填写</span>';
		            		  }else{
		            			return data;
		            		  }
		            	  }
		              },*/
		              { 'mDataProp':'status',"bSortable": false,"sClass": "center","sTitle":"状态",
		            	  "mRender": function ( data, type, row ) {
			            		if(data==1){
			            			return '<span style="color:green;">在职</span>';
			            		}else{
			            			return '<span style="color:red;">离职</span>';
			            		}
		            	  }
		              },
		             /* { 'mDataProp':'createDate',"bSortable": false,"sClass": "center","sTitle":"创建日期",
		            	  "mRender": function ( data, type, row ) {
			            		return covertDate(data);
		            	  }
		              },*/
		              { 'mDataProp':'id',"bSortable": false,"sClass": "center","sTitle":"操作",
		            	  "mRender": function ( data, type, row ) {
		            		return '<span><a href="javascript:showUpdateForm(\''+data+'\')">更改</a></span><span><a href="javascript:del(\''+data+'\');">删除</a></span>';
		            	  }
		              }
		              ],
          "aLengthMenu": [
                          [5, 10, 15, 20,50,100],
                          [5, 10, 15, 20,50,100] // change per page values here
                          ],
          "fnCreatedRow": function( nRow, aData, iDisplayIndex ) {
              $(nRow).on('click', function (){//行click击事件
              	if($(this).hasClass("active")){
              		$(this).removeClass("active");
              	}else{
              		$('#employee_table tbody tr').removeClass("active");
              		var set = jQuery(this).attr("data-set");
              		$(this).toggleClass("active");
              		jQuery.uniform.update(set);
              	}
              	dataArray.push(aData);
              });   
          },
          "bRetrieve": true, 
          "iDisplayLength": 15,
          "sPaginationType": "bootstrap",
          "oLanguage": {
        	  "sProcessing": '<i class="fa fa-coffee"></i>&nbsp;正在加载中......',
        	  "sEmptyTable" : "没有查询到数据",
        	  "sLengthMenu": "每页展示    _MENU_  条 ",
        	  "sInfo": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
        	  "sInfoEmpty":"当前显示0到0条，共0条记录",
        	  "oPaginate": {
        		  "sFirst": "首页",
        		  "sPrevious": "上一页 ",
        		  "sNext": "下一页",
        		  "sLast": "末页",
        	  }
          },
          "fnInitComplete": function() {
              this.fnAdjustColumnSizing(true);
           }

	});

	jQuery('#employee_table .group-checkable').change(function () {
		var set = jQuery(this).attr("data-set");
		var checked = jQuery(this).is(":checked");
		jQuery(set).each(function () {
			if (checked) {
				$(this).attr("checked", true);
			} else {
				$(this).attr("checked", false);
			}
			$(this).parents('tr').toggleClass("active");
		});
		jQuery.uniform.update(set);

	});

	jQuery('#employee_table tbody tr .checkboxes').change(function(){
		$(this).parents('tr').toggleClass("active");
	});

	/*jQuery('#employee_table_wrapper .dataTables_filter input').addClass("form-control input-medium"); // modify table search input
	jQuery('#employee_table_wrapper .dataTables_length select').addClass("form-control input-xbig"); // modify table per page dropdown
	jQuery('#employee_table .dataTables_length select').select2(); // initialize select2 dropdown
	jQuery('#employee_table_processing').css("text-align","center");*/

}

//点击新增员工
function showAddView(){
	$("#header_bar").html("添加员工");
	$("#one_form")[0].reset();
	$("#addEmployee").modal();
}

//点击更新员工信息
function showUpdateForm(id){
	//console.log(id);
	//console.log("length"+dataArray.length);
	$("#header_bar").html("更新员工信息");
	$("#one_form")[0].reset();//
	var i=0;
	for(;i<dataArray.length;i++){
	  if(dataArray[i].id==id){
		  $("#id").val(id);
		  $("#name").val(dataArray[i].name);
		  $("#departmentId").val(dataArray[i].depId);
		  $("#sex").val(dataArray[i].sex);
		  $("#age").val(dataArray[i].age);
		  $("#tel").val(dataArray[i].tel);
		  $("#mobile").val(dataArray[i].mobile);
		  $("#email").val(dataArray[i].email);
		  //$("#qq").val(dataArray[i].qq);
		  //$("#weixin").val(dataArray[i].weixin);
		  if(dataArray[i].status==1){
			  $("#status").val(1);
		  }else{
			  $("#status").val(0);
		  }
		  $("#bak").val(dataArray[i].bak);
	  }
	}
	dataArray=[];
	//$("#name").val(item.name);
	$("#addEmployee").modal();
}

//删除数据
function del(id){
	$("#delId").val(id);
	$("#delEmployee").modal();
}

//点击查询按键
function showSearchView(){
	$("#searchEmployee").modal();
}


             